<?php
/**
 * 年检记录类
 * @Author    紫梦
 * @DateTime  2018-01-17
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\Cars;
use app\common\model\Inspect as InspectModel;
use app\common\request\Oss;

class Inspect extends Base
{
	/**
     * 年检列表
     */
    public function index()
    {
        $model = InspectModel::all();
        $this->assign('data',$model);

        return $this->fetch();
    }

    /**
     * 添加年检记录
     */
    public function add()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            if($data){               
	            $car = Cars::get(['car_unique_id'=>$data['car_unique_id']]);
	            if(!$car){
	                $msg['status'] = 1;
	                $msg['msg'] = '车架号不存在,请查正后再添加';
	                return $msg;
	            }
            } 
            $data['car_id'] = $car['car_id'];           
            $data['car_sn'] = $car['car_sn'];           
            $model = new InspectModel();                 
            $model->allowField(true)->save($data);

            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('inspect_file')->insert(['inspect_id'=>$model->id,'file_url'=>$url]);
                    }
                }

            }   
            if($model->id){
                $msg['msg'] = '添加成功';                
            }else{
                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }
            return $msg;

        }else{
            return $this->fetch();

        }

    }

    /**
     * 编辑年检记录
     */
    public function edit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            
            $model = new InspectModel();
            $res = $model->allowField(true)->update($data);

            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('inspect_file')->insert(['inspect_id'=>$data['id'],'file_url'=>$url]);
                    }
                }

            } 
            if($res){

                $msg['msg'] = '编辑成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '编辑失败,请稍后再试';
            }

            return $msg;
        }else{

            $model = InspectModel::get($id);
            
            
            $this->assign('data',$model);
            
            return $this->fetch();

        }
    }

    /**
     * 删除年检记录
     */
    public function delete($id)
    {
        $msg['status'] = 0;
        $res = InspectModel::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }
}